window.eventBus = new Vue()

import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css'
import VueRouter from 'vue-router'
import VueResource from 'vue-resource'
import { Loading } from 'element-ui'
import axios from 'axios'

// 引入css样式文件
import './assets/css/style.css'
import './assets/font/fonts/iconfont.css'
import './assets/font/fonts1/iconfont.css'

// 引入vue-amap
import AMap from 'vue-amap'

//引入基础路由和项目路由
import EipRouter from './router/eip-router.js'

import API from './api/base_api.js'
import axiosAPI from './api/api.js'

global.API = API
global.axiosAPI = axiosAPI

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(VueResource)
// Vue.use(axios)
Vue.use(AMap)
Vue.prototype.$axios = axios

Vue.prototype.setCookie = function(c_name, value, expiredays) {
	var exdate = new Date()
	exdate.setDate(exdate.getDate() + expiredays)
	document.cookie =
		c_name +
		'=' +
		escape(value) +
		(expiredays == null ? '' : ';expires=' + exdate.toGMTString())
}

Vue.prototype.getCookie = function(c_name) {
	if (document.cookie.length > 0) {
		var c_start = document.cookie.indexOf(c_name + '=')
		if (c_start != -1) {
			c_start = c_start + c_name.length + 1
			var c_end = document.cookie.indexOf(';', c_start)
			if (c_end == -1) c_end = document.cookie.length
			return unescape(document.cookie.substring(c_start, c_end))
		}
	}
	return ''
}

// 初始化vue-amap
AMap.initAMapApiLoader({
	// 高德的key
	key: '1caf7e2255d40bceac95775754061197',
	// 插件集合
	plugin: [
		'AMap.Autocomplete',
		'AMap.Geocoder',
		'AMap.PlaceSearch',
		'AMap.Scale',
		'AMap.OverView',
		'AMap.ToolBar',
		'AMap.MapType',
		'AMap.PolyEditor',
		'AMap.CircleEditor',
	],
})

Vue.http.interceptors.push(function(request, next) {
	//post提交数据的时候，给限制一下下一次的提交间隔
	var loadingInstance
	if (request.method == 'POST') {
		var div = document.getElementById('tab-content-div')
		console.log(div)
		loadingInstance = Loading.service({ fullscreen: true })
	}

	if (
		Vue.prototype.getCookie('token') == null ||
		Vue.prototype.getCookie('token') == ''
	) {
		this.$router.push({ path: '/login' })
	}

	// request.headers.set('Authorization', sessionStorage.Authorization)
	// request.headers.set('userId', localStorage.userId)
	// //重新把cookie设置生效时间
	// Vue.prototype.setCookie('token', Vue.prototype.getCookie('token'), 1 / 24)
	// Vue.prototype.setCookie('userId', Vue.prototype.getCookie('userId'), 1 / 24)
	// request.headers.set('Authorization', Vue.prototype.getCookie('token'))
	// request.headers.set('userId', Vue.prototype.getCookie('userId'))

	request.headers.set('Authorization', sessionStorage.Authorization)
	request.headers.set('userId', localStorage.userId)
	//重新把cookie设置生效时间
	// Vue.prototype.setCookie("token", Vue.prototype.getCookie("token"), 1/24);
	// Vue.prototype.setCookie("userId", Vue.prototype.getCookie("userId"), 1/24);
	// request.headers.set('Authorization', Vue.prototype.getCookie("token"));
	// request.headers.set('userId', Vue.prototype.getCookie("userId"));

	next(function(response) {
		if (loadingInstance != null) {
			loadingInstance.close()
		}

		console.log('回来的数据是' + response.body.status)
		if (response.body.status == -104) {
			this.$router.push({ path: '/login' })
		}
	})
})

const routes = EipRouter

/**
 * 新建一个路由，引入路由表中的地址
 */
const router = new VueRouter({
	base: '/admin/',
	routes,
})

router.beforeEach((to, from, next) => {
	console.log('Authorization', sessionStorage.Authorization)

	if (sessionStorage.Authorization == null) {
		if (to.path == '/login') {
			next()
		} else {
			next('/login')
		}
	}
	next()
})

new Vue({
	el: '#app',
	router: router,
})
